<?php

namespace app\student\controller;
use app\student\security\StuCheck as StuCheckLogic;
use app\student\security\Auth as AuthLogic;
use app\student\model\Thesis as ThesisModel;
use app\student\model\Select as SelectLogic;
use think\Controller;
use think\Session;

class SelectThesis extends Controller
{
    /**
     * 选择题目
     * @access public
     * @return mixed 返回登录视图
     */
    public function selectThesis($tisId='',$tisKey=''){
        $stuCheck = new StuCheckLogic();
        //检测学生登录情况
        if ($stuCheck->check()) {
            //检测是否有审核员权限
            $auth = new AuthLogic();
            //获取学号
            $username = Session::get('token')['stuUsername'];
            //是否有查看题目的权限
            if ($auth->check('selectmore', $username)) {
                //是否在选题阶段
                if(getSrvConfig()['srvStatus']==4) {
                    $select=new SelectLogic();
                    //是否已经选过课
                    if(sizeof($select::where('stuUsername',$username)->find())===0){
                        //是否被选择
                        if(sizeof($select::where('tisId',$tisId)->find())===0){
                            $thesis=new ThesisModel();
                            if($result=$thesis::where('tisId',$tisId)->find()){
                                //授权码是否正确
                                if($result['tisKey']==$tisKey){
                                    if($select->save(['tisId'=>$tisId,'stuUsername'=>$username])){
                                        return 1;
                                    }else{
                                        return "选题失败，请稍后再试";
                                    }
                                }else{
                                    return "授权码出错，选题失败";
                                }
                            }else{
                                return "选择的题目不存在";
                            }
                        }else{
                            return "该题目已经被人选择了";
                        }
                    }else{
                        return "你已经选择了题目，请别重复选择";
                    }
                }else{
                    return "目前不是选题阶段";
                }
            }else{
                return "错误500：您没有选择题目的权限";
            }
        }else{
            return "错误600：您的登录信息出现问题，请重新登录"
            . "<br>" . "1.您的帐号在其他地方登录"
            . "<br>" . "2.您在线时间过长（5小时以上）"
            . "<br>" . "3.令牌验证错误";
        }
    }
}